package sanrenxing.dayEnd.backRepertoryTotal.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sanrenxing.dayEnd.backRepertoryTotal.dao.TransactionDao;
import sanrenxing.dayEnd.backRepertoryTotal.domain.*;
import sanrenxing.dayEnd.backRepertoryTotal.service.TransactionService;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @desrciption 业务处理 证券交易数据
 * @auth：yidu 李小锋
 * @date:2021/3/12 0012 - 14:09
 * @version:1:0
 */
@Service("TransactionService")
public class TransactionServiceImpl implements TransactionService {
    //自动装配
    @Autowired
    private TransactionDao transactionDao;
    /**
     * 新增一行数据
     *
     * @param transaction 对象
     * @return 0、增加失败 1、增加成功
     */
    @Override
    public int add(Transaction transaction) {
        //取出交易金额
        double totalMoney=transaction.getTranNum()*transaction.getPrice();
        double trueMoney=0;
        if(transaction.getMay().equals("买入")){
            //查询出费率
            ExchangeVarietyRate rate = transactionDao.queryEx(transaction.getSeId());
            //结算金额
           trueMoney=totalMoney+((totalMoney*rate.getStaDuty())+(totalMoney*rate.getTranFee())+
                    (totalMoney*rate.getCollFee())+(totalMoney*rate.getHandFee()));
           transaction.setMark(-1);
        }else if(transaction.getMay().equals("卖出")){
            //取出总金额
            //查询出费率
            ExchangeVarietyRate rate = transactionDao.queryEx(transaction.getSeId());
            //结算金额
            trueMoney=totalMoney-((totalMoney*rate.getStaDuty())+(totalMoney*rate.getTranFee())+
                    (totalMoney*rate.getCollFee())+(totalMoney*rate.getHandFee()));
            transaction.setMark(1);
        }
        //添加交易金额
        transaction.setTranMoney(totalMoney);
        //添加结算金额
        transaction.setSettMoney(trueMoney);
        return transactionDao.add(transaction);
    }

    /**
     * 删除一行数据
     *
     * @param tranId 编号
     * @return 0、删除失败 1、删除成功
     */
    @Override
    public int delete(String tranId) {
        return transactionDao.delete(tranId);
    }

    /**
     * 修改一行数据
     * @param transaction 对象
     * @return 0、修改失败 1、修改成功
     */
    @Override
    public int update(Transaction transaction) {
        //取出交易金额
        double totalMoney=transaction.getTranNum()*transaction.getPrice();
        double trueMoney=0;
        if(transaction.getMay().equals("买入")){
            //查询出费率
            ExchangeVarietyRate rate = transactionDao.queryEx(transaction.getSeId());
            //结算金额
            trueMoney=totalMoney+((totalMoney*rate.getStaDuty())+(totalMoney*rate.getTranFee())+
                    (totalMoney*rate.getCollFee())+(totalMoney*rate.getHandFee()));
            transaction.setMark(-1);
        }else if(transaction.getMay().equals("卖出")){
            //取出总金额
            //查询出费率
            ExchangeVarietyRate rate = transactionDao.queryEx(transaction.getSeId());
            //结算金额
            trueMoney=totalMoney-((totalMoney*rate.getStaDuty())+(totalMoney*rate.getTranFee())+
                    (totalMoney*rate.getCollFee())+(totalMoney*rate.getHandFee()));
            transaction.setMark(1);
        }
        //添加交易金额
        transaction.setTranMoney(totalMoney);
        //添加结算金额
        transaction.setSettMoney(trueMoney);
        return transactionDao.update(transaction);
    }

    /**
     * 查询所有的订单数据
     *
     * @return 返回一个订单集合
     */
    @Override
    public List<Transaction> findAll() {
        return transactionDao.findAll();
    }

    /**
     * 分页查询
     *
     * @param start     开始行号
     * @param rows      每页行数
     * @param condition 查询条件
     * @return 集合
     */
    @Override
    public List<Transaction> findByPage(int start, int rows, String condition) {
        int pages=((rows-1)*start)+1;
        return transactionDao.findByPage(pages,start+pages,condition);
    }

    /**
     * 查询券商
     *
     * @return
     */
    @Override
    public List<SettingOfSecuritiesLXF> findSettingOf() {
        return transactionDao.findSettingOf();
    }

    /**
     * 查询基金
     *
     * @return
     */
    @Override
    public List<FundSetupLXF> findFund() {
        return transactionDao.findFund();
    }

    /**
     * 品种设置
     *
     * @return
     */
    @Override
    public List<ExchangeVarietyRate> ExchangeVarietyRateLXF() {
        return transactionDao.ExchangeVarietyRateLXF();
    }

    /**
     * 席位设置
     *
     * @return
     */
    @Override
    public List<TradingSeatLXF> TradingSeatLXF() {
        return transactionDao.TradingSeatLXF();
    }

    /**
     * 证券设置
     *
     * @return
     */
    @Override
    public List<Financialsecurities> SecuritiesInfomationLXF() {
        return transactionDao.SecuritiesInfomationLXF();
    }

    /**
     * 按条件统计出记录行数
     *
     * @param condition 条件
     * @return 记录行数
     */
    @Override
    public int count(String condition) {
        return transactionDao.count(condition);
    }

    /**
     * 获取查询条件
     *
     * @param transaction 对象
     * @return 条件字符串
     */
    @Override
    public String getcondition(Transaction transaction) {
        //定义查询条件字符串，初值为带空格的字符串(注意后续条件中的空格)
        String condition =" ";
        //如果员工对象为空，则直接返回空字符串
        if(transaction==null){
            return condition;
        }
        //如果交易编号不等于null
        if(transaction.getTranId()!=null &&transaction.getTranId().trim().length()!=0){
            condition+="and tranId like '%"+transaction.getTranId()+"%'";
        }
        //交易日期
        if(transaction.getTranDate()!=null){
            //为了是日期能够让oracle识别
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            condition+="and TranDate=to_date('"+sdf.format(transaction.getTranDate())+"','yyyy-MM-dd')";
        }
        //数量
        if (transaction.getTranNum()!=0){
            condition+="and TranNum like '%"+transaction.getTranNum()+"%'";
        }
        //单价
        if (transaction.getPrice()!=0){
            condition+="and Price like '%"+transaction.getPrice()+"%'";
        }
        //结算金额
        if (transaction.getSettMoney()!=0){
            condition+="and settMoney like '%"+transaction.getSettMoney()+"%'";
        }
        //交易金额
        if (transaction.getTranMoney()!=0){
            condition+="and tranMoney like '%"+transaction.getTranMoney()+"%'";
        }
        //计算日期
        if (transaction.getSettDate()!=null){
            condition+="and settDate like '%"+transaction.getSettDate()+"%'";
        }
        //基金ID
        if(transaction.getFundId()!=null &&transaction.getFundId().trim().length()!=0){
            condition+="and fundId like '%"+transaction.getFundId()+"%'";
        }
        //证券ID
        if(transaction.getSeId()!=null &&transaction.getSeId().trim().length()!=0){
            condition+="and seId in ("+transaction.getSeId()+")";
        }
        //券商ID
        if(transaction.getStockId()!=null &&transaction.getStockId().trim().length()!=0){
            condition+="and stockId like '%"+transaction.getStockId()+"%'";
        }
        //席位ID
        if(transaction.getSeatId()!=null &&transaction.getSeatId().trim().length()!=0){
            condition+="and seatId like '%"+transaction.getSeatId()+"%'";
        }
        //现金账户ID
        //交易方式
        if(transaction.getMay()!=null &&transaction.getMay().trim().length()!=0){
            condition+="and may ="+transaction.getMay()+"";
        }
        //证券利息
        if (transaction.getSeInte()!=0){
            condition+="and seInte like '%"+transaction.getSeInte()+"%'";
        }
        //交易状态
        if (transaction.getTranStatus()!=0){
            condition+="and tranStatus like '%"+transaction.getTranStatus()+"%'";
        }
        if (transaction.getMark()!=0){
            condition+="and mark= "+transaction.getMark();
        }
        return condition;
    }

    @Override
    public int addExcl(TransactionExcl transactionExcl) {
        return transactionDao.addExcl(transactionExcl);
    }
}
